package com.cqut.forum.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cqut.forum.entity.User;
import com.cqut.forum.service.ActiveService;
import com.cqut.forum.service.ModuleService;
import com.cqut.forum.service.PictureService;
import com.cqut.forum.service.UserService;

/**
 * 欢迎界面action
 * @author yangxun
 */
@Controller
public class WelcomeAction {
	@Resource
	private UserService userService;
	@Resource
	private ActiveService articleService;
	@Resource
	private ModuleService moduelService;
	@Resource
	private PictureService pictureService;
	
	@RequestMapping("/forum/welcome_all_data")
	@ResponseBody
	public Map<String, Object> welcomeAllData(HttpServletRequest request,int index,int size){
		Map<String, Object> result = new HashMap<String, Object>();
		//通知
		result.put("notice", null);
		//博客
		result.put("article", welcomeArticleData(index, size));
		//动态
		result.put("dongtai", welcomeDongtaiData(index, size));
		
		User user = (User)request.getSession().getAttribute("user");
		if(user!=null){
			result.put("isLogin", true);
		}
		else{
			result.put("isLogin", false);
		}
		
		result.put("systemStyle", request.getSession().getServletContext().getAttribute("ss"));
		
		return result;
	}
	
	/**
	 * 获取热门信息和最新用户
	 * @param index
	 * @param size
	 * @return
	 */
	@RequestMapping("/forum/welcome_hot_data")
	@ResponseBody
	public Map<String, Object> welcomeHotData(int index,int size){
		//热门博主
		List<Map<String, Object>> hotUser = userService.getMostScore(index,6);
		//热门博客
		List<Map<String, Object>> hotArticle = articleService.getHotArticle(index, size);
		//新加入的用户
		List<User> fishUser = userService.fishUser(index,5);
		List<Map<String, Object>> fu = new ArrayList<Map<String,Object>>();
		for(User u : fishUser){
			Map<String, Object> m = new HashMap<String, Object>();
			
			m.put("userId", u.getUserId());
			m.put("userPhoto", u.getUserPhoto());
			m.put("userName", u.getUserName());
			m.put("userSex", u.getUserSex());
			
			fu.add(m);
		}
		//热门图片
		List<Map<String, Object>> hotPicture = pictureService.getHotPicture(index, size);
				
		Map<String, Object> result = new HashMap<String, Object>();
		
		result.put("hotUser", hotUser);
		result.put("hotArticle", hotArticle);
		result.put("fishUser", fu);
		result.put("hotPicture", hotPicture);
		
		return result;
	}
	
	@RequestMapping("/forum/welcome_article_data")
	@ResponseBody
	public List<Map<String, Object>> welcomeArticleData(int index,int size){
		int moduleId = moduelService.getModuleIndex("博客");
		List<Map<String, Object>> articles = articleService.getLastArticle(index, size,moduleId);
		
		return articles;
	}
	@RequestMapping("/forum/welcome_dongtai_data")
	@ResponseBody
	public List<Map<String, Object>> welcomeDongtaiData(int index,int size){
		int moduleId = moduelService.getModuleIndex("动态");
		List<Map<String, Object>> articles = articleService.getLastArticle(index, size,moduleId);
		
		return articles;

	}
	
	
}
